
<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.custom-tree-node {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex: 1;
}
</style>
<!-- tsx -->
<template>
    <div class="package">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-tickets"></i> 品 牌 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input
                    clearable
                    v-model="query.name"
                    style="width: 200px; margin: 0 20px 0 0"
                    placeholder="名称"
                    class="handle-input mr10"
                ></el-input>
                <!-- <el-select class="handle-input mr10" 
                    style="width: 200px; margin: 0 20px 0 0" clearable v-model="query.treeNo"></el-select> -->
                <el-button type="primary" @click="getList">搜索</el-button>
                <el-button type="warning" @click="showadd = true">新增</el-button>
                <el-button type="danger" @click="deleteAllId">批量删除</el-button>
            </div>
            <el-table @selection-change="handleSelectionChange" :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column
                type="selection"
                width="55pxn">
                </el-table-column>
                <el-table-column prop="name" label="品牌名称" align="center">
                    <!-- <template slot-scope="scope">
                    <span v-if="scope.row.accountTypeName == 10" style="color:#ed1941">支出</span>
                    <span v-if="scope.row.accountTypeName == 20" style="color:#45b97c">收入</span>
                  </template> -->
                </el-table-column>
                <el-table-column prop="resourceImg" label="资源图片" width="150" align="center">
                    <template slot-scope="scope">
                        <el-image
                            style="border-ridius: 50%; margin: 0 5px; width: 50px; height: 50px"
                            :src="scope.row.resourceImg"
                            :preview-src-list="[scope.row.resourceImg]"
                        >
                        </el-image>
                  </template>
                </el-table-column>
                <el-table-column prop="levelOrder" label="排序"  align="center"></el-table-column>
                <el-table-column prop="createdOn" label="创建时间"  align="center"></el-table-column>
                <el-table-column label="操作" width="150" align="center" >
                    <template slot-scope="scope">
                        <el-button size="mini" type="warning" @click="editDetail(scope.row)">修改</el-button>
                        <el-button size="mini" type="danger" @click="deleteOne(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    @size-change="sizeChange"
                    @current-change="pageChange"
                    :current-page="query.pageIndex"
                    :page-sizes="[20, 50, 100, 200]"
                    :page-size="query.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageTotal"
                >
                </el-pagination>
            </div>
        </div>

        <el-dialog title="新增品牌" :visible.sync="showadd" width="400px">
            <el-form ref="send" :model="addFrom" label-width="100px">
                <el-form-item label="资源图:">
                    <el-upload
                        action="http://47.108.208.252:810/api/File/Upload"
                        list-type="picture-card"
                        :show-file-list="false"
                        :data="{ fileCategory: 10 }"
                        :on-success="handleAvatarSuccess2"
                    >
                        <el-image v-show="addFrom.resourceImg" style="width: 100px; height: 100px" :src="addFrom.resourceImg"> </el-image>
                        <i class="el-icon-plus" v-show="!addFrom.resourceImg"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="名称:">
                    <el-input v-model="addFrom.name" style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="排序:">
                    <el-input v-model="addFrom.levelOrder" style="width: 200px"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="showadd = false">取 消</el-button>
                <el-button type="primary" @click="addOne">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="修改品牌" :visible.sync="showEdit" width="400px">
            <el-form ref="send" :model="editFrom" label-width="100px">
                <el-form-item label="资源图:">
                    <el-upload
                        action="http://47.108.208.252:810/api/File/Upload"
                        list-type="picture-card"
                        :show-file-list="false"
                        :data="{ fileCategory: 10 }"
                        :on-success="handleAvatarSuccess3"
                    >
                        <el-image v-show="editFrom.resourceImg" style="width: 100px; height: 100px" :src="editFrom.resourceImg"> </el-image>
                        <i class="el-icon-plus" v-show="!editFrom.resourceImg"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="名称:">
                    <el-input v-model="editFrom.name" style="width: 200px"></el-input>
                </el-form-item>
                <el-form-item label="排序:">
                    <el-input v-model="editFrom.levelOrder" style="width: 200px"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="showEdit = false">取 消</el-button>
                <el-button type="primary" @click="editOne">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script >
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
import { PagingBrand,AddBrand,UpdateBrand,DeleteOneBrand,DeleteMore} from '@/api/brand';
export default {
    // import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
        // 这里存放数据
        return {
            query: {
                pageIndex: 1,
                pageSize: 20,
                name: '',
                treeNo: ''
            },
            addTree: false,
            tableData: [],
            pageTotal: 0,
            showadd:false,
            addFrom:{},
            editFrom:{},
            showEdit:false,
            deleteArr:[]
        };
    },
    // 过滤器
    filters: {},
    // 监听属性 类似于data概念
    computed: {},
    // 监控data中的数据变化
    watch: {},
    // 方法集合
    methods: {
        handleSelectionChange(e) {
            console.log(e)
            this.deleteArr = e.map(item => item.id)
        },
        editOne() {
            UpdateBrand(this.editFrom).then(res => {
                if (res.RequestStatus == 200) {
                    this.$message.success('成功');
                    this.getList();
                    this.showEdit = false;
                    this.editFrom = {};
                }})
        },
        editDetail(row){
            this.editFrom = row
            this.showEdit = true
        },
        deleteOne(row){
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                    DeleteOneBrand({ id: row.id }).then((res) => {
                            this.$message.success('成功');
                            this.getList();
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        deleteAllId() {
            console.log(this.deleteArr)
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    DeleteMore({ ids: this.deleteArr }).then((res) => {
                            this.$message.success('成功');
                            this.getList();
                    });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        addOne() {
            AddBrand(this.addFrom).then(res =>{
                if (res.RequestStatus == 200) {
                    this.$message.success('成功');
                    this.getList();
                    this.showadd = false;
                    this.addFrom = {};
                }})
        },
        handleAvatarSuccess2(file) {
            // this.addFrom.resourceImg = this.$url + file.Data.url;
            this.$set(this.addFrom, 'resourceImg', this.$url + file.Data.url);
        },
        handleAvatarSuccess3(file) {
            // this.addFrom.resourceImg = this.$url + file.Data.url;
            this.$set(this.editFrom, 'resourceImg', this.$url + file.Data.url);
        },
        pageChange(e) {
            this.query.pageIndex = e;
            this.getList();
        },
        getList() {
            PagingBrand(this.query).then((res) => {
                this.tableData = res.Data.list;
                this.pageTotal = res.Data.paging.totalItemCount;
            });
        },
        sizeChange(e) {
            this.query.pageSize = e;
            this.getList();
        },
        searchDetail(row) {}
    },
    // 生命周期 - 创建完成（可以访问当前this实例）
    created() {
        this.getList();
    },
    // 生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, // 生命周期 - 创建之前
    beforeMount() {}, // 生命周期 - 挂载之前
    beforeUpdate() {}, // 生命周期 - 更新之前
    updated() {}, // 生命周期 - 更新之后
    beforeDestroy() {}, // 生命周期 - 销毁之前
    destroyed() {}, // 生命周期 - 销毁完成
    ctivated() {} // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>